package com.ltt.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ltt.annotation.NoAuth;
import com.ltt.common.UserThreadLocal;
import com.ltt.dao.pojo.*;
import com.ltt.dao.vo.BlogVo;
import com.ltt.dao.vo.Result;
import com.ltt.dao.vo.params.BlogParams;
import com.ltt.dao.vo.params.BlogUpdateParams;
import com.ltt.dao.vo.params.DeleteBlogParams;
import com.ltt.service.AttentionService;
import com.ltt.service.BlogService;
import com.ltt.service.LabelService;
import com.ltt.service.UserInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;

/**
 * @author ‘李天泰’
 * @version 1.0
 * @description: TODO
 * @date 2023/5/10 21:48
 */
@CrossOrigin
@Api(tags = "帖子模块")
@RestController
@RequestMapping("/blog")
public class BlogController {
    @Autowired
    private BlogService blogService;

    @Autowired
    private LabelService labelService;

    @Autowired
    private UserInfoService userInfoService;

    @Autowired
    private AttentionService attentionService;

    @ApiOperation("获取所有标签")
    @GetMapping("/getLabel")
    public Result getLabel() {
        List<Label> label = labelService.findLabel();
        return Result.successWithData(label);
    }

//    @ApiOperation("发帖子走搜索引擎")
//    @PostMapping("/insert")
//    public Result insertBlog(@RequestBody BlogParams blogParams) {
//        Blog blog = new Blog();
//        BeanUtils.copyProperties(blogParams, blog);
//        blog.setOpenid(UserThreadLocal.get().getOpenid());
//        blogService.insertBlog(blog);
//
//        Blog blog1 = blogService.findBlog(blog.getId());
//
//        try {
//            ES.insertBlog(blog1);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//
//        return Result.successWithData(blog.getId());
//    }


    @ApiOperation("通过id查找帖子")
    @PostMapping("/findBlogById")
    @NoAuth
    public Result findBlogById(int id) {
        Blog blog = blogService.findBlog(id);
        BlogVo blogVo = new BlogVo();
        BeanUtils.copyProperties(blog,blogVo);
        setOtherInfo(blogVo);
        return Result.successWithData(blog);
    }

    @ApiOperation("通过openid查找帖子")
    @PostMapping("/findBlogByOpenId")
    public Result findBlogByOpenId(String openid) {
        List<Blog> blog = blogService.findBlogByOpenid(openid);
        List<BlogVo> blogVos = new ArrayList<>();
        for (Blog blog1 : blog) {
            BlogVo blogVo = new BlogVo();
            BeanUtils.copyProperties(blog1,blogVo);
            setOtherInfo(blogVo);

            blogVos.add(blogVo);
        }
        return Result.successWithData(blogVos);
    }

//    @ApiOperation("通过内容查找帖子")
//    @PostMapping("/findBlogByContent")
//    public Result findBlogByContent(String content) {
//        try {
//            List<Blog> blogList = ES.findBlogByContent(content);
//            return Result.successWithData(blogList);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        return Result.failed("出问题了");
//    }
//
//    @ApiOperation("更新帖子")
//    @PostMapping("/updateBlog")
//    public Result updateBlog(@RequestBody BlogUpdateParams params) {
//        Date date = new Date();
//        SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
//
//        Blog blog = new Blog();
//        BeanUtils.copyProperties(params,blog);
//
//        blog.setOpenid(UserThreadLocal.get().getOpenid());
//        blog.setUpdateTime(dateFormat.format(date));
//        blogService.updateBlog(blog);
//
//        Blog esBlog = blogService.findBlog(blog.getId());
//
//        try {
//            ES.updateBlog(esBlog);
//            return Result.successWithoutData();
//        }catch (Exception e){
//            e.printStackTrace();
//        }
//        return Result.failed("出问题了");
//    }


    @ApiOperation("直接获取所有博客，不走搜索引擎")
    @PostMapping("/getAllBlog")
    public Result getAllBlog(){
        List<Blog> allBlog = blogService.getAllBlog();
        List<BlogVo> blogVos = new ArrayList<>();
        for (Blog blog1 : allBlog) {
            BlogVo blogVo = new BlogVo();
            BeanUtils.copyProperties(blog1,blogVo);
            System.out.println(blogVo);
            if(blogVo != null){
                setOtherInfo(blogVo);

                blogVos.add(blogVo);
            }

        }
        return Result.successWithData(blogVos);
    }


    @ApiOperation("直接获取所有博客，不走搜索引擎")
    @PostMapping("/getAllBlogs")
    @NoAuth
    public Result getAllBlogs(){
        List<Blog> allBlog = blogService.getAllBlog();
        return Result.successWithData(allBlog);
    }


    @ApiOperation("通过内容搜索博客，不走搜索引擎")
    @PostMapping("/findBlogByContentNotES")
    public Result findBlogByContentNotES(@RequestParam String content){
        List<Blog> allBlog = blogService.findBlogByContent(content);
        List<BlogVo> blogVos = new ArrayList<>();
        for (Blog blog1 : allBlog) {

            BlogVo blogVo = new BlogVo();
            BeanUtils.copyProperties(blog1,blogVo);
            setOtherInfo(blogVo);

            blogVos.add(blogVo);
        }
        return Result.successWithData(blogVos);
    }

    @ApiOperation("通过内容搜索博客，不走搜索引擎ss")
    @PostMapping("/getAllBlogsBy")
    @NoAuth
    public Result getAllBlogsBy(@RequestBody String content){
        JSONObject jsonObject =  JSON.parseObject(content);
        HashSet<Blog> allBlog = blogService.getAllBlogsBy(jsonObject.getString("openid"), jsonObject.getString("content"),jsonObject.getString("label"));

        return Result.successWithData(allBlog);
    }

    @ApiOperation("发帖子不走搜索引擎")
    @PostMapping("/insertBlogNotEs")
    public Result insertBlogNotEs(@RequestBody BlogParams blogParams) {
        Blog blog = new Blog();
        BeanUtils.copyProperties(blogParams, blog);
        blog.setOpenid(UserThreadLocal.get().getOpenid());
        blogService.insertBlog(blog);


        return Result.successWithData(blog.getId());
    }

    @ApiOperation("更新帖子不走搜索引擎")
    @PostMapping("/updateBlogNotEs")
    public Result updateBlogNotEs(@RequestBody BlogUpdateParams params) {
        Date date = new Date();
        SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");

        Blog blog = new Blog();
        BeanUtils.copyProperties(params,blog);

        blog.setOpenid(UserThreadLocal.get().getOpenid());
        blog.setUpdateTime(dateFormat.format(date));
        blogService.updateBlog(blog);


        return Result.failed("出问题了");
    }

    @ApiOperation("点赞博客")
    @PostMapping("/addLikesBlog")
    public Result addLikesBlog(@RequestParam int blogId){
        Likes likes = new Likes();
        likes.setBlogId(blogId);
        likes.setUserId(UserThreadLocal.get().getOpenid());
        blogService.addLikes(likes);
        return Result.successWithoutData();
    }

    @ApiOperation("取消点赞博客")
    @PostMapping("/cancelLikesBlog")
    public Result cancelLikesBlog(@RequestParam int blogId){
        Likes likes = new Likes();
        likes.setBlogId(blogId);
        likes.setUserId(UserThreadLocal.get().getOpenid());
        blogService.cancelLikes(likes);
        return Result.successWithoutData();
    }


    private void setOtherInfo(BlogVo blog){
        Likes likes = new Likes();
        likes.setBlogId(blog.getId());
        System.out.println(UserThreadLocal.get());
        try {
            if(UserThreadLocal.get().getOpenid() == null){
                return;
            }
        } catch (Exception e){
            return;
        }

        System.out.println("11");
        Likes likeBlog = blogService.findLikeBlog(likes);

        if(likeBlog != null){
            blog.setLike(1);
        }else {
            blog.setLike(0);
        }

        User userById = userInfoService.findUserById(blog.getOpenid());
        blog.setUserPic(userById.getUserPic());
        blog.setUserName(userById.getNickName());

        Attention attention = new Attention();
        attention.setFromOpenid(UserThreadLocal.get().getOpenid());
        attention.setToOpenid(blog.getOpenid());

        Attention attention1 = attentionService.isAttention(attention);
        if(attention1 == null){
            blog.setIsAttention("false");
        }else {
            blog.setIsAttention("true");
        }
    }

    @ApiOperation("删除博客")
    @PostMapping("/deleteBlog")
    public Result deleteBlog(@RequestParam int blogId){
        System.out.println(blogId);
        DeleteBlogParams deleteBlogParams = new DeleteBlogParams();
        deleteBlogParams.setBlogId(blogId);
        deleteBlogParams.setOpenid(UserThreadLocal.get().getOpenid());
        blogService.deleteBlog(deleteBlogParams);
        return Result.successWithoutData();
    }

    @ApiOperation("删除博客")
    @PostMapping("/deleteBlogs")
    @NoAuth
    public Result deleteBlogs(@RequestBody String blogId){
        System.out.println(blogId);
        JSONObject jsonObject = JSONObject.parseObject(blogId);
        System.out.println(Integer.parseInt(jsonObject.getString("blogId")));
        DeleteBlogParams deleteBlogParams = new DeleteBlogParams();
        deleteBlogParams.setBlogId(Integer.parseInt(jsonObject.getString("blogId")));
        deleteBlogParams.setOpenid("admin");
        blogService.deleteBlog(deleteBlogParams);
        return Result.successWithoutData();
    }

}
